package reflet;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectSample {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        Class rc = Class.forName("reflet.Robot");
        Robot r = (Robot) rc.newInstance();
        /*得到方法*/
        Method getHello = rc.getDeclaredMethod("hello", String.class);
        /*给私有方法设置*/
        getHello.setAccessible(true);
        Object str = getHello.invoke(r, "sun");
        System.out.println(str);
        Field name = rc.getDeclaredField("name");
        name.setAccessible(true);
        name.set(r, "sunlei");
        Method getSay = rc.getMethod("say", String.class);
        getSay.invoke(r, "hello");

    }
}
